import mailbox
import mido
import sys


def tran(x):
    if x >= 'a':
        return ord(x) - 87
    elif x == '0':
        return 0.5
    else:
        return float(x)

def NmnRe(n,h,r,l,k,pt):
    print(n)
    print(h)
    print(r)
    print(l)
    print(k)
    print('--------path------')
    print(pt)

    print(3)
    m = mido.MidiFile()
    t = mido.MidiTrack()
    print(4)
    m.tracks.append(t)
    print(5)
    t.append(mido.Message('program_change', program=12))
    print(6)
    key = [60, 62, 64, 65, 67, 69, 71]
    symbol = '!@#$%^&'
    delay = 0
    print(7)
    for i in range(len(l)):

        for j in range(len(n[int(l[i])])):
            # print(8)
            p = int(240 * tran(r[int(l[i])][j]))
            # print("9"+"-"+str(n[int(l[i])][j]))
            if n[int(l[i])][j] == '0':
                delay = p
            else:
                x = symbol.find(n[int(l[i])][j])
                # print(x)
                if x == -1:
                    x = key[int(n[int(l[i])][j]) -1]
                    # print(x)
                else:
                    x = key[x]+1
                    # print(x)

                x += (int(h[int(l[i])][j]) -4) *12
                # print(x)
                x += int(k[i])
                # print(x)
                t.append(mido.Message('note_on',note=x,time=delay,velocity=127))
                t.append(mido.Message('note_off',note=x,time=p,velocity=127))
                delay = 0
                # print(12)
    print(8)
    m.save(pt)
    print(9)
def getlist(n):
    Str = sys.argv[int(n)]
    List = Str.replace(" ","")[1:-1].split(",")
    return List
if __name__ == '__main__':
    print(1)
    note = getlist(1)
    pitch = getlist(2)
    meter = getlist(3)
    component = getlist(4)
    tone = list(sys.argv[5])
    path = sys.argv[6]
    print(2)
    NmnRe(note,pitch,meter,component,tone,path)
    print(10)


